package JDBCwork;

import java.sql.Date;
import java.sql.SQLException;
import java.util.Scanner;

public class UI {

    public static void main(String[] args) {
        PeopleService servie=  new PeopleService();
        Scanner sc=new Scanner(System.in);
        while(true) {
            showUI();
            int choose=sc.nextInt();
            switch (choose){
                case 1:
                    try {
                        servie.findAll();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    String num=sc.next();
                    String name=sc.next();
                    String gander=sc.next();
                    String day=sc.next();
                    double hight =sc.nextDouble();
                    Date date = DateUtils.StringToDate(day);
                    while(date==null){
                        date = DateUtils.StringToDate(sc.next());
                    }
                    boolean result=servie.add(new People());
                    if(result){
                        System.out.println("yes");
                    }else{
                        System.out.println("no");
                    }
                    break;
                case 3:
                    People p = servie.findByName(sc.next());
                    System.out.println("请输入要修改的日期和身高");
                    day=sc.next();
                    hight=sc.nextDouble();
                    p.setDate(DateUtils.StringToDate(day));
                    p.setHight(hight);
                    boolean update=servie.update(p);
                    if(update){
                        System.out.println("yes");
                    }else{
                        System.out.println("no");
                    }

                    break;
                case 4:
                    System.out.println("请输入要删除的学生的姓名");
                    People p1 = servie.findByName(sc.next());
                    if(p1==null){
                        System.out.println("查无此人");
                    }else{
                        boolean b= servie.delete(p1);
                        if(b){
                            System.out.println("删除成功");
                        }else {
                            System.out.println("删除失败");
                        }
                    }

                    break;
                case 5:
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入错误请重新输入");
            }
        }
    }

    private static void showUI() {

        System.out.println("1:查询");
        System.out.println("2:添加");
        System.out.println("3:修改");
        System.out.println("4:删除");
        System.out.println("5:退出");
    }
}